<?php

namespace Tcnet\Pay\Gateways\Ttpay;

use Tcnet\Pay\Events;
use Tcnet\Pay\Exceptions\GatewayException;
use Tcnet\Pay\Exceptions\InvalidArgumentException;
use Tcnet\Pay\Exceptions\InvalidConfigException;
use Tcnet\Pay\Exceptions\InvalidSignException;
use Tcnet\Pay\Gateways\Ttpay\Support;
use Yansongda\Supports\Collection;

class MiniappGateway extends Gateway
{
    /**
     * Pay an order.
     *
     * @author xiaozan <i@xiaozan.me>
     *
     * @param string $endpoint
     *
     * @throws GatewayException
     * @throws InvalidArgumentException
     * @throws InvalidConfigException
     * @throws InvalidSignException
     *
     * @see https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/pay-list/pay
     */
    public function pay($endpoint, array $payload): Collection
    {
        $payload['sign'] = Support::generateSign($payload);

        Events::dispatch(new Events\PayStarted('Ttpay', 'Miniapp', $endpoint."create_order", $payload));
        return Support::requestApi("create_order",$payload);
    }
}
